#encoding:utf8
from django import forms
import sys
sys.path.append('/cihi/app/include')
import init

k = init.INIT()
def getProject():
    result = []
    for project in k.getProjectList():
        result.append((project,k.getProjectAtt(project).get('projectalias')))
    return result

def getMonitor():
    result = []
    for monitor in k.getMonitorList():
        result.append((monitor,k.getMonitorAtt(monitor).get('monitoralias')))
    return result
 
class ClientForm(forms.Form):
    def __init__(self,*args,**kwargs):
        super(ClientForm,self).__init__(*args,**kwargs)
        self.fields['hostname'] = forms.IPAddressField(label='IP地址')
        self.fields['aliasname'] = forms.CharField(label='主机别名')
        self.fields['project'] = forms.MultipleChoiceField(label='项目',choices=getProject(),widget=forms.CheckboxSelectMultiple(),required=False)
        self.fields['username'] = forms.CharField(label='SSH用户',required=False,help_text='编辑主机时，不修改此项则留空')
        self.fields['password'] = forms.CharField(label='SSH密码',required=False,help_text='编辑主机时，不修改此项则留空')
        self.fields['port'] = forms.CharField(label='SSH端口',required=False,help_text='编辑主机时，不修改此项则留空')

class MonitorForm(forms.Form):
    monitorname = forms.CharField(label='监控项目')
    monitoralias = forms.CharField(label='项目别名')
    monitortype = forms.ChoiceField(label='执行方式',choices=(('client','客户端执行'),('server','服务端执行')),widget=forms.Select())
    monitorcmd = forms.CharField(label='调用命令')
    monitorhelp = forms.CharField(label='使用说明',widget=forms.Textarea())


class ProjectForm(forms.Form):
    projectname = forms.CharField(label='项目代码')
    projectalias = forms.CharField(label='项目别名')
    codedir = forms.CharField(label='项目路径')
    codeowner = forms.CharField(label='项目所有者')
    servicename = forms.CharField(label='服务名称')
    ignorelist = forms.CharField(label='忽略更新',required=False) 

class UserForm(forms.Form):
    oldpasswd = forms.CharField(label='原密码',widget=forms.PasswordInput)
    newpasswd = forms.CharField(label='新密码',widget=forms.PasswordInput)
    secpasswd = forms.CharField(label='确认密码',widget=forms.PasswordInput)

    def clean(self):
        cleaned_data = super(UserForm,self).clean()
        newpasswd = cleaned_data.get('newpasswd')
        secpasswd = cleaned_data.get('secpasswd')
        if newpasswd and secpasswd:
            if newpasswd != secpasswd:
                msg = u"两次输入密码不一致。"
                self._errors['secpasswd'] = self.error_class([msg])
            else:
                if len(newpasswd) < 6:
                    msg = u"密码长度过短至少6个字符。"
                    self._errors['secpasswd'] = self.error_class([msg])
        return cleaned_data

class SelectMonitor(forms.Form):
    def __init__(self,*args,**kwargs):
        super(SelectMonitor,self).__init__(*args,**kwargs)
        self.fields['monitor'] = forms.MultipleChoiceField(label='项目',choices=getMonitor(),widget=forms.CheckboxSelectMultiple(),required=False)
    
class ClientMonitor(forms.Form):
    def __init__(self,*args,**kwargs):
        super(ClientMonitor,self).__init__(*args,**kwargs)
        monitorlist = eval(k.getKey('cihimonitor_temp'))
        for monitor in monitorlist:
            monitoratt = k.getMonitorAtt(monitor)
            self.fields[monitor] = forms.CharField(label=monitoratt.get('monitoralias'),help_text=monitoratt.get('monitorhelp'))

class MonitorImage(forms.Form):
    starttime = forms.DateTimeField(label='起始时间')
    endtime = forms.DateTimeField(label='结束时间')
